//
// Created by v on 24-12-24.
//

#include <getopt.h>

#include "rt_print.h"
#include "CMAIServiceInterface.h"
#ifdef RK3588
#include "CMAIServiceImplRk3588.h"
#endif
#include "cm_utils.h"

#define MODEL_PATH "/home/cat/j/res/model/best-yolo11.rknn"
#define LABEL_FILE "/home/cat/j/res/model/webfuture.txt"

static void test_image_recognize(const char *buf, size_t size)
{
    CMAIServiceInterface *anInterface = NULL;
#ifdef RK3588
    anInterface = new CMAIServiceImplRk3588();
#endif
    if (anInterface)
    {
        anInterface->init();
        anInterface->init_model(MODEL_PATH, LABEL_FILE);

        std::vector<CMAIRecogItem> results;
        anInterface->recognize(buf, size, results);
        std::string text_print;
        for (auto &item : results)
        {
            text_print += item.name + " : " + std::to_string(item.score) + "\n";
        }
        rtinfo("%s\n", text_print.c_str());
        anInterface->deinit_model();
        anInterface->deinit();
        delete anInterface;
    }
    else
    {
        rtdebug("anInterface is none\n");
    }
}

int main(int argc, char **args)
{
    rt_print_init(RT_PRINT_ALL, RT_PRINT_STYLE_ALL, RT_COLOR_TRUE, "ai_service_test");
    rtdebug("Sartup: %s:%s\n", __DATE__, __TIME__);

    char *image_file = NULL;
    int c = -1;
    while((c = getopt(argc, args, "f:")) != -1)
    {
        switch (c)
        {
        case 'f':
            image_file = optarg;
            break;
        default:
            rtinfo("Usage: ./%s -f [image_file]\n", args[0]);
            break;
        }
    }

    if (image_file == NULL)
    {
        rterror("Usage: ./%s -h\n", args[0]);
        return 0;
    }
    char *buf = NULL;
    int size = 0;

    cm_read_file_data(image_file, &buf, &size);

    test_image_recognize(buf, size);

    return 0;
}
